<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态组件与异步组件</title>
    <script src="../vue.global.js"></script>
</head>
<body>
<div id="root"></div>
<script>
    const app = Vue.createApp({
        data() {
            return {
                currentItem: "input-item"
            }
        },
        // <input-item v-show="currentItem == 'input-item'"></input-item>
        // <common-item v-show="currentItem == 'common-item'"></common-item>
        /**
         * 根据数据的变化，结合component和keep-alive来动态显示组件
         */
        template: `
          <keep-alive>
            <component :is="currentItem"/>
          </keep-alive>
          <button @click="handleClick">切换</button>
        `,
        methods: {
            handleClick() {
                if (this.currentItem == 'input-item') {
                    this.currentItem = 'common-item'
                } else {
                    this.currentItem = 'input-item'
                }
            }
        }
    });

    // 局部组件
    app.component("input-item", {
        template: `<input />`
    })
    app.component("common-item", {
        template: `<div>hello world</div>`
    })
    const vm = app.mount("#root");
</script>
</body>
</html>